package rsj.sql.product;

import com.rlyy.aibase.utils.base.Node;
import com.rlyy.aibase.utils.base.TableNode;
import rsj.annotation.DynamicSelect;
import rsj.annotation.Param;
import rsj.annotation.Select;
import rsj.annotation.Update;
import rsj.domain.ProductPageQuery;
import rsj.service.ProductService;

/**
 * 产品-dao
 *
 * @author yuanqinglong
 * @date 2021/05/26
 * @since 2021/5/26 11:58
 */
public interface ProductInfoDao {


    /**
     * 产品列表
     *
     * @param query 查询
     * @return {@link TableNode}
     */
    @DynamicSelect(type = ProductService.class, method = "productListSql")
    TableNode productListPage(ProductPageQuery query);

    /**
     * 数据市场产品列表
     *
     * @param query 查询
     * @return {@link TableNode}
     */
    @DynamicSelect(type = ProductService.class, method = "dataMarketProductListSql")
    TableNode dataMarketProductListPage(ProductPageQuery query);


    /**
     * 统计列表
     *
     * @param query 查询
     * @return {@link TableNode}
     */
    @DynamicSelect(type = ProductService.class, method = "countProductSql")
    Node countProduct(ProductPageQuery query);


    /**
     * 所有产品
     *
     * @return {@link TableNode}
     */
    @Select(value = "SELECT DISTINCT prod_id,prod_uid,prod_name,prod_type_fid,prty_fname " +
            "FROM sys.sys_prod LEFT JOIN sys.sys_prty ON prod_type_fid = prty_fid WHERE prod_status = '1' ORDER BY prod_uid ASC")
    TableNode allProducts();


    /**
     * 帐户配置产品
     *
     * @param orgId org id
     * @return {@link TableNode}
     */
    @Select(value = "SELECT DISTINCT prod_id ,sys_org_prod.prod_uid,prod_name,org_id,charge_mode,charge_price,prod_type_fid,prty_fname FROM sys.sys_org_prod LEFT JOIN sys.sys_prod ON sys_prod.prod_uid = sys_org_prod.prod_uid\n" +
            "LEFT JOIN sys.sys_prty ON prty_fid = sys_prod.prod_type_fid WHERE org_id = #{orgId} AND prod_status = '1' ORDER BY sys_org_prod.prod_uid ASC")
    TableNode accountConfigurationProducts(@Param(value = "orgId") String orgId);


    /**
     * 应用产品通过org id
     *
     * @param orgId  org id
     * @param prodId 促使id
     * @return {@link Node}
     */
    @Select(value = "SELECT orgp_status FROM sys.sys_orgp WHERE orgp_prod_id = #{prodId} AND orgp_org_id = #{orgId} AND orgp_type = '3'")
    Node applyingProductByOrgId(@Param(value = "orgId") String orgId, @Param(value = "prodId") String prodId);


    /**
     * 更新产品销售量
     *
     * @param sales  销售量
     * @param prodId 促使id
     */
    @Update(value = "UPDATE sys.sys_prod SET prod_sales = prod_sales + #{sales} WHERE prod_id = #{prodId}")
    void updateProductSales(@Param(value = "sales") Integer sales, @Param(value = "prodId") String prodId);
}
